package com.vcixp.install.at;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import com.vcixp.install.adapter.DbAdapter;
import com.vcixp.install.adapter.DbProcessor;
import com.vcixp.install.adapter.InstallState;
import com.vcixp.main.login.entity.User;

@At("/install")
@IocBean
public class InstallAt {
	@At("/next")
	@Ok("jsp:install.next")
	public void next() {
	}

	@At("/test")
	@Ok("json")
	public String test(HttpServletRequest request) {
		String type = request.getParameter(DbAdapter.TYPE);
		Map<String, String> map = DbProcessor.getparam(request);
		if (DbProcessor.test(type, map)) {
			return "1";
		} else {
			return "0";
		}
	}

	@At("/createDb")
	@Ok("jsp:install.step2")
	public void createDb(HttpServletRequest request, String username, String password, String systemname) {
		String type = request.getParameter(DbAdapter.TYPE);
		User admin = new User();
		admin.setUsername(username);
		admin.setPassword(password);
		Map<String, String> map = DbProcessor.getparam(request);
		DbProcessor.initDatabase(type, map, admin, systemname);
	}

	@At("/installstate")
	@Ok("json")
	public Map<String, Object> installstate() {
		Map<String, Object> result = new HashMap<>();
		if (InstallState.isInstalled()) {
			result.put("over", "1");
		} else {
			result.put("over", "0");
		}
		result.put("info", InstallState.getInstallinfo());
		return result;
	}
	@At("/finish")
	@Ok("jsp:install.finsh")
	public void finish() {
		
	}
}
